package com.example.wly.myapplication;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ImageView;

public class ColorCombinActivity extends AppCompatActivity {
//成员
    int red=0;//颜色分量初值=0；
    int green=0;
    int blue=0;
    private EditText etGreen;
    private EditText etRed;
    private EditText etBlue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color_combin);
        //找控件

        final ImageView imcolor=(ImageView) findViewById(R.id.im_color);
              EditText etRed=(EditText)findViewById(R.id.et_red);

        final EditText etGreen=(EditText) findViewById(R.id.et_green);
        final EditText etBlue=(EditText) findViewById(R.id.et_blue);
        //监听文本的数据改变
        etBlue.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String blueString=etBlue.getText().toString();
                if(blueString.equals(""))
                {
                    blue=0;
                }else {
                    int querry=Integer.parseInt(blueString);
                    if(querry>=255) {
                        blue = 255;
                    }else{
                        blue=querry;
                    }
                }
                imcolor.setBackgroundColor(Color.rgb(red,green,blue));

            }
        });
        etGreen.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String GreenString=etGreen.getText().toString();
                if(GreenString.equals(""))
                {
                    green=0;
                }else {
                    int querry=Integer.parseInt(GreenString);
                    if(querry>=255) {
                        green = 255;
                    }else{
                        green=querry;
                    }
                }
                imcolor.setBackgroundColor(Color.rgb(red,green,blue));

            }
        });
         TextWatcher tw=new MyWatcher(etRed,imcolor) ;//接口不能new对象，因为有抽象方法
        etRed.addTextChangedListener(tw);


    }

    class MyWatcher implements TextWatcher{
        EditText redEit;
        ImageView imcolor;
        public MyWatcher(EditText r,ImageView i){
            redEit=r;
            imcolor=i;
        }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            String redString=redEit.getText().toString();
            red=Integer.parseInt(redString);
            imcolor.setBackgroundColor(Color.rgb(red,green,blue));
        }
    }


}
